// pages/withdraw/withdraw.js
const app = getApp();
const utils = require('../../utils/util.js');
const post = utils.post;
const showModal = utils.showModal;
const getCurrentPageUrlWithArgs = utils.getCurrentPageUrlWithArgs;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    z_uid: '',
    m_uid: '',
    list: [],
    tx: '',
    money: '',
    tx_money: '',
    name: '',
    first_tixian: '',
    ifName: true, // false代表显示姓名输入框，true隐藏姓名输入框
    tishi: '首次提现请输入姓名',
    cid: 0,
    placeholder: '请输入您的姓名'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.cash_show();
  },
  cash_show() {
    let that = this;
    let uid = wx.getStorageSync('z_uid');
    this.setData({
      z_uid: wx.getStorageSync('z_uid')
    });
    if (uid) {
      let p = {
        m: 'tixian',
        uid: uid
      }
      post(p).then(res => {
        //console.log(res)
        if (res.phone_check == 0) {
          var url = encodeURIComponent('/pages/withdraw/withdraw')
          wx.navigateTo({
            url: '/pages/phoneJ/phoneJ?org_url=' + url
          })
        } else {
          that.setData({
            list: res,
            money: res.promotion_can_cash,
            name: res.weixin_name,
            first_tixian: res.first_tixian
          })
        }

      });
      let p2 = {
        m: 'put_money',
        uid: uid
      }
      post(p2).then(res => {
        //console.log(res)
        that.setData({
          tx: res.result
        })
      })
    }
  },

  cash_click(event) {
    let that = this;
    var name = that.data.name;
    var tx_money = event.target.dataset.money;
    if (name && name.trim().length > 0) {
      that.setData({
        ifName: true,
        tx_money: tx_money
      });

      that.cash(tx_money * 100, name);
    } else {
      that.setData({
        tishi: '首次提现请输入姓名',
        ifName: false,
        tx_money: tx_money
      });
    }
  },

  cash(money, name) {
    wx.showLoading({
      title: '提现处理中',
    });

    let that = this;
    let p = {
      a: 'cash',
      m: 'tixian',
      uid: that.data.z_uid,
      weixin_name: name,
      money: money
    };

    post(p).then(res => {
      wx.hideLoading();

      if (res.r == 0) {
        showModal(that, {
          content: res.errmsg,
          showCancel: false,
          ok_text: '我知道了',
        });

        that.cash_show();
      } else {
        var cid = res.cid;
        if (cid && cid > 0) {
          that.setData({
            cid: cid
          });
          var err_code = res.err_code;
          if (err_code && err_code.length > 0) {
            if ('NAME_MISMATCH' == err_code) { // 姓名不对
              that.setData({
                ifName: false,
                tishi: '姓名不匹配，请重新输入'
              });
            } else if ('NOTENOUGH' == err_code) {
              showModal(that, {
                content: res.errmsg,
                showCancel: false,
                ok_text: '我知道了',
              });
            } else {
              that.cash_retry(cid, name);
            }
          } else {
            showModal(that, {
              content: res.errmsg,
              showCancel: false,
              ok_text: '我知道了',
            });
          }
        } else {
          showModal(that, {
            content: res.errmsg,
            showCancel: false,
            ok_text: '我知道了',
          });
        }
      }
    });
  },

  cash_retry(cid, name) {
    wx.showLoading({
      title: '提现处理中，请稍候',
    });

    let that = this;
    let p = {
      uid: that.data.z_uid,
      a: 'retry',
      m: 'tixian',
      cid: cid
    };

    if (name && name.length > 0) {
      p.weixin_name = name;
    }

    post(p).then(res => {
      wx.hideLoading();

      if (res.r == 0) {
        showModal(that, {
          content: res.errmsg,
          showCancel: false,
          ok_text: '我知道了',
        });

        that.cash_show();
      } else {
        var cid = res.cid;
        if (cid && cid > 0) {
          that.setData({
            cid: cid
          });
          var err_code = res.err_code;
          if (err_code && err_code.length > 0) {
            if ('NAME_MISMATCH' == err_code) { // 姓名不对
              that.setData({
                ifName: false,
                tishi: '姓名不匹配，请重新输入'
              });
            } else if ('NOTENOUGH' == err_code) {
              showModal(that, {
                content: res.errmsg,
                showCancel: false,
                ok_text: '我知道了',
              });
            } else {
              that.cash_retry(cid, name);
            }
          } else {
            showModal(that, {
              content: res.errmsg,
              showCancel: false,
              ok_text: '我知道了',
            });
          }
        } else {
          showModal(that, {
            content: res.errmsg,
            showCancel: false,
            ok_text: '我知道了',
          });
        }
      }
    });
  },

  // 输入名称
  setValue: function (m) {
    let that = this;
    //console.log(m.detail.value)
    that.setData({
      name: m.detail.value
    })
  },

  inputNameFocus: function (m) {
    this.setData({
      placeholder: ''
    });
  },

  inputNameBlur: function (m) {
    this.setData({
      placeholder: '请输入您的姓名'
    });
  },

  // 确定
  confirm: function () {
    let that = this;
    if (0 == that.data.name.length) {
      wx.showToast({
        title: '姓名不能为空',
        icon: 'none',
        duration: 2000,
        mask: true
      });

      return false;
    } else {
      var tx_money = that.data.tx_money;
      var name = that.data.name;
      var cid = that.data.cid;
      if (cid && cid > 0) {
        that.cash_retry(cid, name);
      } else {
        that.cash(tx_money * 100, name);
      }
      that.setData({
        ifName: true
      });
    }
  },
  // 取消
  cancel: function () {
    let that = this;
    that.setData({
      ifName: true
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  submitFormId(e) {
    let formId = e.detail.formId;
    let button = e.detail.target.dataset.name;
    var m_uid = wx.getStorageSync("m_uid");
    let page = getCurrentPageUrlWithArgs();
    let p = {
      m: 'colform',
      uid: m_uid,
      openid: wx.getStorageSync('weapp_openid'),
      formId: formId,
    }
    if (page && page.length > 0) {
      p.page = page;
    }
    if (button && button.length > 0) {
      p.button = button;
    }
    //console.log(p);
    post(p).then(res => {
    })
  },
})